package com.splant.android.smart.UILevel.Community;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.splant.android.R;
import com.splant.android.smart.CommunicationLevel.sPlantAsyncHttpClient;
import com.splant.android.smart.CommunicationLevel.sPlantJsonHttpResponseHandler;
import com.splant.android.smart.ModelLevel.PlantListBean;
import com.splant.android.smart.ModelLevel.PlantTypeListBean;
import com.splant.android.smart.UILevel.Community.fragments.Garden_PlantList_Adapter;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Garden_PlantList_Activity extends Activity implements View.OnClickListener {

    private Button btn_goBack;
    private TextView tv_plantTypeName;
    private sPlantAsyncHttpClient sPlantClient;
    private LRecyclerView lrv_plantList;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private Garden_PlantList_Adapter garden_plantList_adapter;
    private List<PlantListBean.Plant> plantList = new ArrayList<>();
    private PlantTypeListBean.PlantType plantType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.garden_plantlist_activity);

        btn_goBack = (Button) findViewById(R.id.btn_goBack);
        tv_plantTypeName= (TextView) findViewById(R.id.tv_plantTypeName);
        lrv_plantList= (LRecyclerView) findViewById(R.id.lrv_plantList);

        init();

        btn_goBack.setOnClickListener(this);
    }
    private void init()
    {
        plantType= (PlantTypeListBean.PlantType) getIntent().getSerializableExtra("plantType");
        tv_plantTypeName.setText(plantType.getPlantTypeName());

        sPlantClient=sPlantAsyncHttpClient.getInstance();

        garden_plantList_adapter = new Garden_PlantList_Adapter(Garden_PlantList_Activity.this);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(garden_plantList_adapter);

        lrv_plantList.setHasFixedSize(true);
        lrv_plantList.setLayoutManager(new GridLayoutManager(this, 3));
        lrv_plantList.setAdapter(lRecyclerViewAdapter);
        lrv_plantList.setRefreshProgressStyle(ProgressStyle.BallClipRotateMultiple);

        lrv_plantList.setFooterViewColor(R.color.colorAccent, R.color.garden_color2 ,android.R.color.white);
        lrv_plantList.setFooterViewHint("拼命加载中","已经全部为你呈现了","网络不给力啊，点击再试一次吧");
        lrv_plantList.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);

        lrv_plantList.setOnRefreshListener(onRefreshListener);

        lrv_plantList.setOnLoadMoreListener(onLoadMoreListener);

        garden_plantList_adapter.setOnItemClickListener(new Garden_PlantList_Adapter.onItemClickListener() {
            @Override
            public void onItemChick(int pos) {

                PlantListBean.Plant plant= garden_plantList_adapter.getDataList().get(pos);

                Intent intent=new Intent(Garden_PlantList_Activity.this,Garden_Plant_Activity.class);

                intent.putExtra("plantId",plant.getPlantId());

                startActivity(intent);

            }
        });

        getDatas();
    }

    private static final int REQUEST_COUNT = 15;

    public void getDatas() {

        RequestParams params = new RequestParams();
        params.put("plantTypeId",plantType.getPlantTypeId());
        params.put("pageNo",pageNo);
        params.put("pageSize",REQUEST_COUNT);

        sPlantClient.get(sPlantAsyncHttpClient.GETPLANTLIST, params, new sPlantJsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                try
                {
                    if (response.getInt("succeed") == 1)
                    {
                        Gson gson = new Gson();

                        PlantListBean plantListBean = gson.fromJson(response.toString(), PlantListBean.class);

                        if (plantListBean.getPlantList() != null&&plantListBean.getPlantList().size()>0) {

                            plantList.addAll(plantListBean.getPlantList());

                            garden_plantList_adapter.setDataList(plantList);

                            if (plantListBean.getPlantList().size()<REQUEST_COUNT)
                                lrv_plantList.setNoMore(true);

                        }
                        else
                            lrv_plantList.setNoMore(true);

                        lrv_plantList.refreshComplete(REQUEST_COUNT);
                    }
                }
                catch (Exception e)
                {
                    lrv_plantList.refreshComplete(REQUEST_COUNT);
                }
            }
            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                lrv_plantList.refreshComplete(REQUEST_COUNT);
                Toast.makeText(Garden_PlantList_Activity.this, getString(R.string.system_request_error), Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.btn_goBack:
                finish();
                break;
        }
    }

    private OnRefreshListener onRefreshListener=new OnRefreshListener() {
        @Override
        public void onRefresh() {
            pageNo=0;
            plantList.clear();
            getDatas();
        }
    };

    private int pageNo = 0;
    private OnLoadMoreListener onLoadMoreListener=new OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            pageNo += 1;
            getDatas();
        }
    };
}
